package com.atguigu.rabbitmq.four;

import com.atguigu.rabbitmq.util.RabbitmqUtil;
import com.rabbitmq.client.Channel;

import java.util.UUID;

/**
 * @author CodingBee
 * @since 2021/6/25/13:02
 * 发布确认模式   用时
 * 1.单个确认
 * 2.批量确认
 * 3.异步批量确认
 */
public class ConfirmMessage {
    //批量发消息的个数
    public static final int MESSAGE_COUNT = 1000;

    public static void main(String[] args) throws Exception {
        //单个确认
        publishMessageIndividual();//发送1000条消息耗时239毫秒
        //批量确认
        //异步批量确认

    }

    /**
     * 单个确认
     */
    public static void publishMessageIndividual() throws Exception {
        Channel channel = RabbitmqUtil.getChannel();
        //队列的声明
        String queueName = UUID.randomUUID().toString();
        channel.queueDeclare(queueName, Boolean.TRUE, false, false, null);
        //开始发布
        channel.confirmSelect();
        //开始时间
        long l = System.currentTimeMillis();
        //批量发消息
        for (int i = 0; i < MESSAGE_COUNT; i++) {
            channel.basicPublish("", queueName, null, (i + "").getBytes());
            //单个消息马上确认
            boolean b = channel.waitForConfirms();
            if (b) {
                System.out.println("消息发送成功" + i);
            }
        }
        System.out.println(String.format("发送%s条消息耗时%s毫秒", MESSAGE_COUNT, System.currentTimeMillis() - l));
    }
}
